package cn.tedu.mall.search.service.impl;


import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.pojo.product.model.Spu;
import cn.tedu.mall.pojo.search.entity.SpuEntity;
import cn.tedu.mall.pojo.search.entity.SpuForElastic;
import cn.tedu.mall.product.service.front.IForFrontSpuService;
import cn.tedu.mall.search.repository.SpuForElasticRepository;
import cn.tedu.mall.search.service.ISearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//@Service
@Slf4j
public class SearchLocalServiceImpl implements ISearchService {

    @Autowired
    private SpuForElasticRepository spuForElasticRepository;

    @DubboReference
    private IForFrontSpuService forFrontSpuService;

    @Override
    public JsonPage<SpuEntity> search(String keyword, Integer page, Integer pageSize) {
        return null;
    }

    @Override
    public void loadSpuByPage() {
        //使用先循环再判断的循环结构do-while
        int i = 1;
        int pages = 0;
        do {
            JsonPage<Spu> spus = forFrontSpuService.getSpuByPage(i,2);
            List<SpuForElastic> esSpus = new ArrayList<>();
            for (Spu spu : spus.getList()) {
                SpuForElastic esSpu = new SpuForElastic();
                BeanUtils.copyProperties(spu,esSpu);
                esSpus.add(esSpu);
            }
            //执行新增
            spuForElasticRepository.saveAll(esSpus);
            i++;
            pages = spus.getTotalPage();
        }while (i <= pages);
    }
}
